<template>
  <div >
    <el-dialog
        title="片区管理"
        :visible.sync="dialogVisible"
        width="50%"
        @close="resetForm"
        custom-class="right-dialog">
      <el-form :model="dialogForm" ref="dialogForm" :rules="rules" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="dialogForm.name" maxlength="50" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item label="父级" prop="parentId">
          <el-select v-model="dialogForm.parentId" clearable placeholder="请选择">
            <el-option
                v-for="item in tree"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="dialogForm.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="dialogForm.sort" placeholder="请输入排序"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getFwAreaStreetList, saveAreaStreetList} from "@/api/house";

export default {
  name: 'CreatTemplates',
  props: {
    isActive: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: false,
      dialogForm: {
        id:null,
        parentId:null,
        areaLevel:null,
        name:'',
        status: 1,
        sort:0,
      },
      tree:[],
      rules:{
        name: [{ required: true, message: '请填写姓名', trigger: ['blur'] }],
        sort: [{ required: true, message: '请填写排序', trigger: ['blur'] }]
      },
    }
  },
  mounted() {
    this.getStreetName()
  },
  methods: {
    // 获取片区与街道
    getStreetName() {
      getFwAreaStreetList().then((res) => {
        this.tree = res
      })
    },
    // 重置表单
    resetForm() {
      this.$refs.dialogForm.resetFields();
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
    },
    submitForm() {
      this.$refs.dialogForm.validate(valid=>{
        if(!valid)return;
        this.saveLoading=true;
        if (this.dialogForm.parentId==='' ||  this.dialogForm.parentId===null) {
          this.dialogForm.areaLevel = 0
        }else {
          this.dialogForm.areaLevel = 1
        }
        saveAreaStreetList(this.dialogForm).then(()=>{
          this.$message.success('保存成功');
          this.closeDialog();
        }).finally(()=>{
          this.saveLoading=false
        })
      })
    },
  }
}
</script>

<style scoped>

</style>
